#ifndef _LULY__SOUNDEFFECT_H_
#define _LULY__SOUNDEFFECT_H_
#include "PLATFORM.h"
#ifdef _ANDROID_PLATFORM__
#include "SDL_mixer.h"
#else
#include <SDL2/SDL_mixer.h>
#endif

#include <list>
#include <stack>
#include <string>
#include <unordered_map>
class _SoundEffect {
  friend class _Luly;

 private:
  std::unordered_map<std::string, Mix_Chunk*> chunk_list;
  int channelnum = MIX_CHANNELS;  //通道的个数
  static void channel_finnish_callback(int channel) {
    //正在播放的数量
    int playingChannels = 0;
    for (int i = 0; i < _SoundEffect::getInstance().channelnum; i++) {
      if (Mix_Playing(i)) {
        playingChannels++;
      }
    }
    //减少通道
    if (_SoundEffect::getInstance().channelnum > 16 && playingChannels == 0) {
      //减为原来的一半
      Mix_AllocateChannels(_SoundEffect::getInstance().channelnum /= 2);
    }
  }

 private:
  _SoundEffect() {}
  void init() { Mix_ChannelFinished(channel_finnish_callback); }

 public:
  static _SoundEffect& getInstance() {
    static _SoundEffect instance;
    return instance;
  }

 public:
  void loadSound(std::string soundId, std::string fileName);
  void playSound(std::string soundId, bool isLoop);
  ~_SoundEffect();
};
#endif